package com.mi.rdd

import org.apache.spark.sql.SparkSession


object patient {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder().appName("MedicalInsurance").master("local").getOrCreate()
    val data = spark.read
      .option("header", "true")
      .csv("data/data.csv")

    val ownExpenseRDD = data
      .select("药品费自费金额_SUM", "检查费自费金额_SUM", "治疗费自费金额_SUM", "手术费自费金额_SUM", "医用材料费自费金额_SUM")
      .rdd
      .map(x => (x(0).toString.toFloat, x(1).toString.toFloat, x(2).toString.toFloat, x(3).toString.toFloat, x(4).toString.toFloat))

    val declaredExpenseDF = data
      .select("药品费申报金额_SUM", "检查费申报金额_SUM", "治疗费申报金额_SUM", "手术费申报金额_SUM", "床位费申报金额_SUM", "成分输血申报金额_SUM", "一次性医用材料申报金额_SUM", "其它申报金额_SUM")
      .rdd
      .map(x => (x(0).toString.toFloat, x(1).toString.toFloat, x(2).toString.toFloat, x(3).toString.toFloat, x(4).toString.toFloat, x(5).toString.toFloat, x(6).toString.toFloat, x(7).toString.toFloat))

    // 平均自费金额中，各项的占比
    val ownExpenseSum = ownExpenseRDD.reduce((a, b) => (a._1+b._1, a._2+b._2, a._3+b._3, a._4+b._4, a._5+b._5))
    val ownExpenseSumAmount = ownExpenseSum._1 + ownExpenseSum._2 + ownExpenseSum._3 + ownExpenseSum._4 + ownExpenseSum._5
    println("平均药品费自费金额占比: " + ownExpenseSum._1 / ownExpenseSumAmount)
    println("平均检查费自费金额占比: " + ownExpenseSum._2 / ownExpenseSumAmount)
    println("平均治疗费自费金额占比: " + ownExpenseSum._3 / ownExpenseSumAmount)
    println("平均手术费自费金额占比: " + ownExpenseSum._4 / ownExpenseSumAmount)
    println("平均医用材料费自费金额占比: " + ownExpenseSum._5 / ownExpenseSumAmount)

    // 平均申报金额中，各项的占比
    val declaredExpenseSum = declaredExpenseDF.reduce((a, b) => (a._1+b._1, a._2+b._2, a._3+b._3, a._4+b._4, a._5+b._5, a._6+b._6, a._7+b._7, a._8+b._8))
    val declaredExpenseSumAmount = declaredExpenseSum._1 + declaredExpenseSum._2 + declaredExpenseSum._3 + declaredExpenseSum._4 + declaredExpenseSum._5 + declaredExpenseSum._6 + declaredExpenseSum._7 + declaredExpenseSum._8
    println("平均药品费申报金额占比: " + declaredExpenseSum._1 / declaredExpenseSumAmount)
    println("平均检查费申报金额占比: " + declaredExpenseSum._2 / declaredExpenseSumAmount)
    println("平均治疗费申报金额占比: " + declaredExpenseSum._3 / declaredExpenseSumAmount)
    println("平均手术费申报金额占比: " + declaredExpenseSum._4 / declaredExpenseSumAmount)
    println("平均床位费申报金额占比: " + declaredExpenseSum._5 / declaredExpenseSumAmount)
    println("平均成分输血申报金额占比: " + declaredExpenseSum._6 / declaredExpenseSumAmount)
    println("平均一次性医用材料申报金额占比: " + declaredExpenseSum._7 / declaredExpenseSumAmount)
    println("平均其它申报金额占比: " + declaredExpenseSum._8 / declaredExpenseSumAmount)

    spark.stop()
  }
}
